//
//  QJudgeVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/7.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QJudgeVC.h"
#import "QJudgeTopCell.h"
#import "QJudgeBottomCell.h"
#import "QAllOrderMoreVC.h"
@interface QJudgeVC ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation QJudgeVC{

    UITableView *_tableView;
    UIButton *_judgeButton;//评价按钮
    NSMutableString *_labelContent;//标签拼接的字符串
    
    BOOL isBad;//差评
    NSString *_badString;//两个星以下的评论内容
    NSArray *_goodTagArr;//好的标签
    NSMutableArray *_goodTagStr;//好的标签字符串
    NSArray *_badTagArr;//坏的标签
    NSMutableArray *_badTagStr;//坏的标签字符串
}

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"评价";
    
    _goodTagStr = [NSMutableArray array];
    _badTagStr = [NSMutableArray array];
    _badString = @"";
    [self createTableView];
    //[self setupForDismissKeyboard];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifiAction) name:kNotificationStarJudge object:nil];
    [self requestTagData];
}

#pragma mark --- 打分的通知
- (void)notifiAction{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    QJudgeTopCell *cell = (QJudgeTopCell *)[_tableView cellForRowAtIndexPath:indexPath];
    if(cell.ratingControl.rating <= 2){
        isBad = YES;
    }else{
        isBad = NO;
    }
    [_tableView reloadData];
}

#pragma mark --- createTableView
- (void)createTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-50) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:@"QJudgeTopCell" bundle:nil] forCellReuseIdentifier:@"QJudgeTopCell"];
    
    _judgeButton = [QViewUtility createButtonWithFrame:CGRectMake(0,kScreenHeight-50, kScreenWidth,50) font:17 title:@"提交" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    [_judgeButton blueStyle];
    [_judgeButton addTarget:self action:@selector(judgeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_judgeButton];
    
    _judgeArr = [NSMutableArray array];
}

#pragma mark--提交评价
- (void)judgeButtonAction{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    QJudgeTopCell *cell = (QJudgeTopCell *)[_tableView cellForRowAtIndexPath:indexPath];
    CLog(@"=====%ld",(long)cell.ratingControl.rating);
    
    _labelContent = [[NSMutableString alloc] init];
    for (int i=0; i<self.judgeArr.count; ++i) {
        if(self.judgeArr.count == 1){
            _labelContent = self.judgeArr[0];
        }else{
            if(i == self.judgeArr.count-1){
                [_labelContent appendString:[NSString stringWithFormat:@"%@",self.judgeArr[self.judgeArr.count-1]]];
            }else{
                [_labelContent appendFormat:@"%@,",self.judgeArr[i]];
            }
        }
    }
    
    if(cell.ratingControl.rating == 0){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请打总体分值" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }else{
        [self requestData:[NSString stringWithFormat:@"%ld",(long)cell.ratingControl.rating]];
    }
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    __weak QJudgeVC *wself = self;
    if(indexPath.row == 0){
        QJudgeTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QJudgeTopCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.moneyStr = self.moneyStr;
        cell.timeStr = self.timeStr;
        cell.addressStr = self.addressStr;
        cell.isClick = YES;
        cell.model = _model;
        return cell;
    }else{
        static NSString *identify = @"cell";
        QJudgeBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if(!cell){
            cell = [[QJudgeBottomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        }
        cell.isClick = YES;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.isBad = isBad;
        if(isBad){
            if(_goodTagStr.count){
                cell.datas = _badTagStr;
            }
        }else{
            if(_badTagStr.count){
                cell.datas = _goodTagStr;
            }
        }
        cell.judgeBlock = ^(NSMutableArray *arr){
            __strong QJudgeVC *sSelf = wself;
            sSelf->_judgeArr = arr;
        };
        
        cell.badBlock = ^(NSString *text){
            __strong QJudgeVC *sSelf = wself;
            sSelf->_badString = text;
            CLog(@"textz====%@",text);
        };
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return 230;
    }else{
        QJudgeBottomCell *cell = (QJudgeBottomCell *)[self tableView:_tableView cellForRowAtIndexPath:indexPath];
        return cell.height;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
//提交评价
- (void)requestData:(NSString *)rating{
    NSString *url = [QParamService urlWithAction:@"order/submitEvaluateOrder"];
    NSDictionary *param = [QParamService judgeOrder:self.orderId score:rating content:_badString type:@"1" labelContent:_labelContent];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"评价成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationjudgeSuccess object:nil];
            for (UIViewController *controller in self.navigationController.viewControllers){
                if ([controller isKindOfClass:[QAllOrderMoreVC class]]){
                    [self.navigationController popToViewController:controller animated:YES];
                }
            }
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }    
    }];
}

#pragma mark--获取评价标签
- (void)requestTagData{
    NSString *url = [QParamService urlWithAction:@"getEvaluationTags"];
    NSDictionary *param = [QParamService getEvaluationTags];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            NSDictionary *dic = basModel.items;
            _goodTagArr = dic[@"listMoreThanThree"];
            for(NSDictionary *d1 in _goodTagArr){
                [_goodTagStr addObject:d1[@"content"]];
            }
            _badTagArr = dic[@"listMoreThanTwo"];
            for(NSDictionary *d2 in _badTagArr){
                [_badTagStr addObject:d2[@"content"]];
            }
            [_tableView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}



-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [_tableView endEditing:YES];
}

@end
